﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinCustControls
{
    public class PaintClass
    {
        /// <summary>
        /// 获取圆角矩形的路径
        /// </summary>
        /// <param name="rect">矩形结构</param>
        /// <param name="r">圆角半径</param>
        /// <returns></returns>
        public static GraphicsPath GetRoundRectangle(Rectangle rect, int r)
        {
            var graphicsPath = new GraphicsPath();

            int l = 2 * r; // 圆弧直径;圆弧所在矩形的边长
            // 上边线段
            graphicsPath.AddLine(rect.X + r, rect.Y, rect.Right - r, rect.Y);
            // 右上角 1/4 圆弧 270 90
            graphicsPath.AddArc(rect.Right - l, rect.Y, l, l, 270F, 90F);
            // 右边竖线
            graphicsPath.AddLine(rect.Right, rect.Y + r, rect.Right, rect.Bottom - r);
            // 右下角 1/4 圆弧 0 90
            graphicsPath.AddArc(rect.Right - l, rect.Bottom - l, l, l, 0F, 90F);
            // 下边线段
            graphicsPath.AddLine(rect.Right - r, rect.Bottom, rect.X + r, rect.Bottom);
            // 左下角 1/4 圆弧 90 90
            graphicsPath.AddArc(rect.X, rect.Bottom - l, l, l, 90F, 90F);
            // 左边竖线
            graphicsPath.AddLine(rect.X, rect.Bottom - r, rect.X, rect.Y + r);
            // 左上角 1/4 圆弧 180 90
            graphicsPath.AddArc(rect.X, rect.Y, l, l, 180F, 90F);

            return graphicsPath;
        }
    }
}
